/************************************************************
 ********************** 左表格*******************************
 ***********************************************************/
/**
 * 基础字典类型管理初始化
 */
var SdsDictype = {
    id: "SdsDictypeTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    typeId:0,
    typeDesc:null
};

/**
 * 初始化表格的列
 */
SdsDictype.initColumn = function () {
    return [
        {field: 'selectItem', radio: true, visible: false},
        {title: '类型代码', field: 'typeCode', align: 'center', valign: 'middle', sortable: true, visible: false},
        {title: '类型描述', field: 'typeDesc', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle', visible: false, formatter:function(value,row,index){
                var _html = '<a href="javascript:SdsDictype.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
                _html += '<a href="javascript:SdsDictype.del(\''+row.id+'\',\''+row.name+'\')" title="作废"><i class="hisui-icon icon-cancel"></i></a>';
                return _html;
            }}];
};

/**
 * 检查是否选中
 */
SdsDictype.check = function () {
    if(SdsDictype.typeId){
        return true;
    } else {
        Sys.info("请先选中某一字典类型！");
        return false;
    }
};

/**
 * 根据字典分类id查找字典详细
 */
SdsDictype.initDicDetail = function (e, treeId, treeNode) {
    SdsDictype.typeId = treeNode.id;
    SdsDictype.typeDesc = treeNode.name;
    SdsDic.search();
}

/**
 * 点击新增基础字典类型
 */
SdsDictype.add = function () {
    var index = Sys.open('新增基础字典类型',Sys.backendPath + '/sds/sdsdictype/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看基础字典类型详情
 */
SdsDictype.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.typeId;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('基础字典类型详情',Sys.backendPath + '/sds/sdsdictype/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 作废基础字典类型
 */
SdsDictype.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.typeId;
        name = this.typeDesc
        flag = true;
    }
    if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsdictype/cancel", function (data) {
                Sys.success("作废成功!");
                SdsDictype.table.refresh();
            }, function (data) {
                Sys.error("作废失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdsDictypeId",id);
            ajax.start();
        };
        Sys.confirm("是否作废基础字典类型?",operation);
    }
};

/**
 * 查询基础字典类型列表
 */
SdsDictype.search = function () {
    var queryData = {};
    queryData['typeDesc'] = $("#typeDesc").val();
    SdsDictype.table.refresh({query: queryData});
};

/************************************************************
 ********************** 右表格部分*******************************
 ***********************************************************/
/**
 * 基础字典管理初始化
 */
var SdsDic = {
    id: "SdsDicTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsDic.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '字典类型', field: 'dicType', align: 'center', valign: 'middle', sortable: true, visible: false},
        {title: '字典代码', field: 'dicCode', align: 'center', valign: 'middle', sortable: true},
        {title: '字典描述', field: 'dicDesc', align: 'center', valign: 'middle', sortable: true},
        {title: '是否有效', field: 'isActive', align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:SdsDic.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
    		_html += '<a href="javascript:SdsDic.del(\''+row.id+'\',\''+row.name+'\')" title="作废"><i class="hisui-icon icon-cancel"></i></a>';
    		return _html;
        }}];
};

/**
 * 检查是否选中
 */
SdsDic.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsDic.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增基础字典
 */
SdsDic.add = function () {
    if(SdsDictype.typeId&&SdsDictype.typeId!=0){
        var index = Sys.open('新增基础字典',Sys.backendPath + '/sds/sdsdic/to_add',['800px', '420px'],function(layero, index, iframeWin){
            $(iframeWin.document).find("#dicType").attr("value", SdsDictype.typeId);
            $(iframeWin.document).find("#typeName").attr("value", SdsDictype.typeDesc);
        });
    }else{
        Sys.info("请先选中某个字典分类！");
        return false;
    }
    this.layerIndex = index;
};

/**
 * 打开查看基础字典详情
 */
SdsDic.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('基础字典详情',Sys.backendPath + '/sds/sdsdic/to_update/' + id + "/",['800px', '420px'], function(layero, index, iframeWin){
            $(iframeWin.document).find("#typeName").attr("value",SdsDictype.typeDesc);
        });
        this.layerIndex = index;
    }
};

/**
 * 作废基础字典
 */
SdsDic.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/sds/sdsdic/cancel", function (data) {
	            Sys.success("作废成功!");
	            SdsDic.table.refresh();
	        }, function (data) {
	            Sys.error("作废失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("sdsDicId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否作废基础字典?",operation);
    }
};

/**
 * 查询基础字典列表
 */
SdsDic.search = function () {
    var queryData = {};
    queryData['dicType'] = SdsDictype.typeId;
    queryData['dicDesc'] = $("#dicDesc").val();
    SdsDic.table.refresh({query: queryData});
};

$(function () {
    var ztree = new $ZTree(SdsDictype.id, Sys.backendPath + "/sds/sdsdictype/tree");
    ztree.bindOnClick(SdsDictype.initDicDetail);
    ztree.init();
    SdsDictype.ztree = ztree;
    fuzzySearch(SdsDictype.id,'#seachKey',null,true);

    var dicColumns = SdsDic.initColumn();
    var dic_t = new BSTable(SdsDic.id, Sys.backendPath + "/sds/sdsdic/list", dicColumns);
    dic_t.setPaginationType("server");
    dic_t.setPageSize(10);
    dic_t.setSubHeight(-10);
    dic_t.setShowExport(false);
    dic_t.setShowRefresh(false);
    dic_t.setShowToggle(false);
    dic_t.setShowColumns(false);
    SdsDic.table = dic_t.init();
});
